

package com.maoshi.shop.distribution.comment.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maoshi.shop.bean.enums.OfflineHandleEventStatus;
import com.maoshi.shop.bean.enums.OfflineHandleEventType;
import com.maoshi.shop.bean.model.OfflineHandleEvent;
import com.maoshi.shop.bean.param.OfflineHandleEventAuditParam;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.distribution.comment.Enums.BasicSetDistributionSwitchEnum;
import com.maoshi.shop.distribution.comment.dao.DistributionBasicSetMapper;
import com.maoshi.shop.distribution.comment.model.DistributionAuditing;
import com.maoshi.shop.distribution.comment.model.DistributionBasicSet;
import com.maoshi.shop.distribution.comment.service.DistributionBasicSetService;
import com.maoshi.shop.service.OfflineHandleEventService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * @author lgh on 2019/04/01.
 */
@Service
@AllArgsConstructor
public class DistributionBasicSetServiceImpl extends ServiceImpl<DistributionBasicSetMapper, DistributionBasicSet> implements DistributionBasicSetService {

    private final DistributionBasicSetMapper distributionBasicSetMapper;
    private final OfflineHandleEventService offlineHandleEventService;

    @Override
    @Cacheable(cacheNames = "DistributionBasicSet", key = "'0'")
    public DistributionBasicSet getByShopId() {
        return distributionBasicSetMapper.getByShopId(Constant.PLATFORM_SHOP_ID);
    }

    @Override
    @CacheEvict(cacheNames = "DistributionBasicSet", key = "'0'")
    public void removeCacheByShopId(Long shopId) {

    }

    @Override
    public IPage<DistributionBasicSet> getDistributionBasicSetPage(PageParam<DistributionAuditing> page, DistributionBasicSet distributionBasicSet) {
        return distributionBasicSetMapper.getDistributionBasicSetPage(page, distributionBasicSet);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditDistributionBasicSet(OfflineHandleEventAuditParam offlineHandleEventAuditParam, Long sysUserId) {
        // 审核通过
        if (Objects.equals(offlineHandleEventAuditParam.getStatus(), OfflineHandleEventStatus.AGREE_BY_PLATFORM.getValue())) {
            // 更新分销功能为未开启状态
            distributionBasicSetMapper.updateDistributionSwitch(offlineHandleEventAuditParam.getHandleId(), BasicSetDistributionSwitchEnum.DISABLE.getValue());
        }
        // 审核不通过
        else if (Objects.equals(offlineHandleEventAuditParam.getStatus(), OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue())) {
            distributionBasicSetMapper.updateDistributionSwitch(offlineHandleEventAuditParam.getHandleId(), BasicSetDistributionSwitchEnum.OFFLINE.getValue());
        }
        // 更新活动
        offlineHandleEventService.auditOfflineEvent(offlineHandleEventAuditParam, sysUserId);
    }

    @Override
    public void offline(DistributionBasicSet distributionBasicSet, String offlineReason, Long sysUserId) {
        // 添加下线处理记录
        Date now = new Date();
        OfflineHandleEvent offlineHandleEvent = new OfflineHandleEvent();
        offlineHandleEvent.setHandleType(OfflineHandleEventType.DISTRIBUTION.getValue());
        offlineHandleEvent.setHandleId(distributionBasicSet.getBasicSetId());
        offlineHandleEvent.setCreateTime(now);
        offlineHandleEvent.setOfflineReason(offlineReason);
        offlineHandleEvent.setHandlerId(sysUserId);
        offlineHandleEvent.setStatus(OfflineHandleEventStatus.OFFLINE_BY_PLATFORM.getValue());
        offlineHandleEvent.setShopId(distributionBasicSet.getShopId());
        offlineHandleEvent.setUpdateTime(now);
        offlineHandleEventService.save(offlineHandleEvent);

        // 更新分销状态为平台下线
        distributionBasicSetMapper.updateDistributionSwitch(distributionBasicSet.getBasicSetId(), BasicSetDistributionSwitchEnum.OFFLINE.getValue());

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditApply(Long eventId, Long basicSetId, String reapplyReason) {
        // 更新活动为待审核状态
        distributionBasicSetMapper.updateDistributionSwitch(basicSetId, BasicSetDistributionSwitchEnum.WAIT_AUDIT.getValue());

        // 更新事件状态
        offlineHandleEventService.updateToApply(eventId, reapplyReason);
    }
}
